import random
def createPSphere():
    theList = []
    for x in range(20):
        theS = cmds.polySphere(n="sp_#")
        cmds.setAttr(theS[0]+'.tx',random.randint(-6,6) )
        cmds.setAttr(theS[0]+'.tz',random.randint(-6,6) )
        cmds.setAttr(theS[0]+'.sx',random.randint(3,5)*0.3 ) 
        cmds.setAttr(theS[0]+'.sy',random.randint(3,5)*0.3 ) 
        cmds.setAttr(theS[0]+'.sz',random.randint(3,5)*0.3 )
        theList.append(theS[0])
    return theList 
    
theSpList = createPSphere() 
bl = cmds.shadingNode('blinn',asShader=1 , n = 'antsCrowd_blinn')
rmp = cmds.shadingNode('ramp',asTexture=1 , n = 'antsCrowd_rmp')
sw = cmds.shadingNode('singleShadingSwitch',asUtility=1 , n= 'antsCrowd_singleSwitch')

cmds.removeMultiInstance(rmp+'.colorEntryList[1]',b=1)
cmds.setAttr(rmp+".colorEntryList[1].color" ,  0.0312047 , 0.0495308 , 0.153841 ) # blue
cmds.setAttr(rmp+".colorEntryList[0].color" ,  0.185 ,0 ,0 )
cmds.setAttr(rmp+".colorEntryList[1].position" , 1 )

cmds.connectAttr(rmp+'.outColor',bl+'.color')
cmds.connectAttr(sw+'.out',rmp+'.uvCoord.vCoord')


#cmds.select(theSpList,r=1)
#cmds.hyperShade( theSpList , assign=bl )
#cmds.select( cl = 1 )


try: cmds.sets(bl+'SG',q=1)
except :     
    cmds.sets(theSpList,r=1,nss=1,em=1,n=bl+'SG')    
    cmds.connectAttr(bl+'.outColor',bl+'SG.surfaceShader',f=1)
cmds.sets(theSpList,e=1,fe=bl+'SG')


for i,x in enumerate(theSpList):
    s=cmds.listRelatives(x,s=1)[0]
    cmds.connectAttr(s+'.instObjGroups[0]',sw+'.input['+str(i)+'].inShape')
    cmds.addAttr(x,ln='vcoord',at='float',min=0,max=1)
    cmds.setAttr(x+'.vcoord',e=1,keyable=1)
    cmds.connectAttr(x+'.vcoord',sw+'.input['+str(i)+'].inSingle',f=1)
    cmds.setAttr(x+'.vcoord',random.uniform(0,1.0))


